package org.netty.demo.lihh.notice.util;

import com.alibaba.fastjson.JSON;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.netty.demo.lihh.notice.domain.ServerMsgProtocol;
import org.netty.demo.lihh.notice.enumeration.ClientStateEnum;

/**
 * 模拟 消息 传输协议
 *
 * @author lihh
 */
public class MsgUtil {
  
  /**
   * 构建 连接成功 服务端响应消息
   *
   * @param channelId socket channel id
   * @param tk        用户凭证
   * @author lihh
   */
  public static TextWebSocketFrame buildConnectSuccessServerResponseMsg(String channelId, String tk) {
    // 构建消息
    ServerMsgProtocol serverMsgProtocol = ServerMsgProtocol.builder()
        .channelId(channelId)
        .tk(tk)
        .state(ClientStateEnum.CLIENT_CONNECT)
        .msg(Constants.CONNECT_SUCCESS_MSG)
        .build();
    
    return new TextWebSocketFrame(JSON.toJSONString(serverMsgProtocol));
  }
  
  /**
   * 构建 登录成功的 ws 消息
   *
   * @param tk        用户 tk
   * @param channelId socket channel
   * @author lihh
   */
  public static TextWebSocketFrame buildLoginResponseMsg(String tk, String channelId) {
    ServerMsgProtocol serverMsgProtocol = ServerMsgProtocol.builder()
        .channelId(channelId)
        .tk(tk)
        .state(ClientStateEnum.CLIENT_LOGIN)
        .msg(String.format(Constants.LOGIN_SUCCESS_MSG, channelId))
        .build();
    
    return new TextWebSocketFrame(JSON.toJSONString(serverMsgProtocol));
  }
  
  /**
   * 构建 退出成功的 ws 消息
   *
   * @param tk        用户 tk
   * @param channelId socket channel
   * @author lihh
   */
  public static TextWebSocketFrame buildLogoutResponseMsg(String tk, String channelId) {
    ServerMsgProtocol serverMsgProtocol = ServerMsgProtocol.builder()
        .channelId(channelId)
        .tk(tk)
        .state(ClientStateEnum.CLIENT_LOGOUT)
        .msg(String.format(Constants.LOGOUT_SUCCESS_MSG, channelId))
        .build();
    
    return new TextWebSocketFrame(JSON.toJSONString(serverMsgProtocol));
  }
}
